﻿/*
Copyright (C) 2011 by Matthew Hickson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Collections;
using System.Text;

namespace thinkschematwo.IO
{
    class GroupedListFormatter
    {

        private const String DEFAULT_COMMENT_FORMAT = "# {0}";
        private const String DEFAULT_GROUP_ITEM_FORMAT = "+{0}";
        private const String DEFAULT_DATA_ITEM_FORMAT = "{0}";

        private thinkschematwo.Collections.GroupedList list;

        private String groupItemFormat;
        private String dataItemFormat;
        private String commentFormat;

        private bool addComments;

        public String GroupItemFormat {
            get { return groupItemFormat; }
            set { groupItemFormat = value; }
        } // GroupItemFormat

        public String DataItemFormat {
            get { return dataItemFormat; }
            set { dataItemFormat = value; }
        } // DataItemFormat

        public String CommentFormat {
            get { return commentFormat; }
            set { commentFormat = value; }
        } // CommentFormat

        // TODO: Not yet implemented...
        public bool AddComments {
            get { return addComments; }
            set { addComments = value; }
        }

        public GroupedListFormatter(thinkschematwo.Collections.GroupedList list)
        {
            this.list = list;

            GroupItemFormat = DEFAULT_GROUP_ITEM_FORMAT;
            DataItemFormat = DEFAULT_DATA_ITEM_FORMAT;
            CommentFormat = DEFAULT_COMMENT_FORMAT;

            AddComments = true;
        } // GroupedListFormatter

        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();

            ArrayList groupkeys = list.GroupKeys;
            groupkeys.Sort();
            foreach (String key in groupkeys)
            {
                if (String.IsNullOrEmpty(key))
                {
                    sb.AppendLine(String.Format(CommentFormat, "globals"));
                }
                else
                {
                    sb.AppendLine(String.Format(GroupItemFormat, key));
                }

                ArrayList datalist = list.GetDataList(key);
                datalist.Sort();
                foreach (String value in datalist)
                {
                    sb.AppendLine(String.Format(DataItemFormat, value));
                }

                sb.AppendLine();
            }

            sb.AppendLine(String.Format(CommentFormat, "eof"));
            sb.AppendLine();

            return sb.ToString();
        } // ToString

    }
}
